package com.pub;

import java.sql.Connection;
import java.sql.SQLException;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.ibatis.sqlmap.engine.execution.SqlExecutor;
import com.ibatis.sqlmap.engine.mapping.statement.RowHandlerCallback;
import com.ibatis.sqlmap.engine.scope.RequestScope;
import com.pub.dialect.DialectInterface;

public class LimitSqlExecutor extends SqlExecutor {
	 private DialectInterface dialect;
	 private boolean enableLimit = true;
	 static final Log logger = LogFactory.getLog(LimitSqlExecutor.class);
	 @Override
	 public void executeQuery(RequestScope request, Connection conn, String sql,
	            Object[] parameters, int skipResults, int maxResults,
	            RowHandlerCallback callback) throws SQLException {
	        if ((skipResults != NO_SKIPPED_RESULTS || maxResults != NO_MAXIMUM_RESULTS)
	                && supportsLimit()) {
	            sql = dialect.getLimitString(sql, skipResults,maxResults);
	            if(logger.isDebugEnabled()){
	                logger.debug(sql);
	            }
	            skipResults = NO_SKIPPED_RESULTS;
	            maxResults = NO_MAXIMUM_RESULTS;            
	        }
	        super.executeQuery(request, conn, sql, parameters, skipResults,
	                maxResults, callback);
	    }

	    public boolean supportsLimit() {
	        if (enableLimit && dialect != null) {
	            return dialect.supportsLimit();
	        }
	        return false;
	    }

		public DialectInterface getDialect() {
			return dialect;
		}

		public void setDialect(DialectInterface dialect) {
			this.dialect = dialect;
		}

		public boolean isEnableLimit() {
			return enableLimit;
		}

		public void setEnableLimit(boolean enableLimit) {
			this.enableLimit = enableLimit;
		}


}
